package com.example.demo.mapper;

import com.example.demo.entity.PageInfo;
import com.example.demo.entity.SendRecordsInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author
 * @Description
 */
@Mapper
@Repository
public interface SendRecordsMapper {

    @Insert("<script>" +
            "INSERT INTO `send_records_info`(`invoice_id`,`email`,`success`,`request_source`,`failed_reason`,`purchase_name`)" +
            "VALUES " +
            "<foreach collection='search.invoiceIdList' item='invoiceId' index='index' separator=',' open=' ' close=';'>" +
            "(#{invoiceId},#{search.email},#{search.success},#{search.requestSource},#{search.failedReason},#{search.purchaseName})" +
            "</foreach>" +
            "</script>")
    Long insertSendRecordsList(@Param("search") SendRecordsInfo sendRecordsInfo);

    @Insert("INSERT INTO `send_records_info`(`invoice_id`,`email`,`success`,`request_source`,`failed_reason`,`purchase_name`)" +
            "VALUES (#{sendRecordsInfo.invoiceId},#{sendRecordsInfo.email},#{sendRecordsInfo.success},#{sendRecordsInfo.requestSource}," +
            "#{sendRecordsInfo.failedReason},#{sendRecordsInfo.purchaseName})")
    @Options(useGeneratedKeys = true, keyProperty = "sendId", keyColumn = "send_id")
    Long insertSendRecords(@Param("sendRecordsInfo") SendRecordsInfo sendRecordsInfo);

    @Select("<script>" +
            "SELECT `send_id`,`invoice_id`,`purchase_name`,`email`,`success`,`request_source`,`failed_reason`,`create_time`,`update_time`" +
            "FROM `send_records_info`" +
            "WHERE 1=1" +
            "<if test='search != null'>" +
            "<if test='search.sendId != null and search.sendId != \"\"'> AND `send_id` = #{search.sendId} </if>" +
            "<if test='search.invoiceId != null and search.invoiceId != \"\"'> AND `invoice_id` LIKE CONCAT('%', #{search.invoiceId}, '%') </if>" +
            "<if test='search.email != null and search.email != \"\"'> AND `email` LIKE CONCAT('%', #{search.email}, '%') </if>" +
            "<if test='search.success != null '> AND `success` = #{search.success} </if>" +
            "<if test='search.requestSource != null and search.requestSource != \"\"'> AND `request_source` = #{search.requestSource} </if>" +
            "<if test='search.purchaseName != null and search.purchaseName != \"\"'> AND `purchase_name` LIKE CONCAT('%', #{search.purchaseName}, '%') </if>" +
            "<if test='search.failedReason != null and search.failedReason != \"\"'> AND `failed_reason` = #{search.failedReason} </if>" +
            "<if test='search.startTime != null and search.startTime != \"\"'> " +
            "AND `create_time` >= #{search.startTime}" +
            "</if>" +
            "<if test='search.endTime != null and search.endTime != \"\"'> " +
            "AND #{search.endTime} >= `create_time`" +
            "</if>" +
            "</if>" +
            "ORDER BY create_time DESC" +
            "<if test='page != null'>" +
            "LIMIT #{page.pageSize} OFFSET #{page.pageOffset}" +
            "</if>" +
            "</script>")
    @Results(id = "sendRecordsInfo", value = {
            @Result(id = true, property = "sendId", column = "send_id"),
            @Result(property = "invoiceId", column = "invoice_id"),
            @Result(property = "purchaseName", column = "purchase_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "success", column = "success"),
            @Result(property = "requestSource", column = "request_source"),
            @Result(property = "failedReason", column = "failed_reason"),
            @Result(property = "reSend", column = "re_send"),
            @Result(property = "reSendId", column = "re_send_id"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
    })
    List<SendRecordsInfo> findSendRecordsList(@Param("search") SendRecordsInfo sendRecordsInfo, @Param("page") PageInfo pageInfo);

    @Select("SELECT `send_id`,`invoice_id`,`purchase_name`,`email`,`success`,`request_source`,`re_send`,`re_send_id`,`create_time`,`update_time`" +
            "FROM `send_records_info`" +
            "WHERE `send_id` = #{sendId}")
    @ResultMap("sendRecordsInfo")
    SendRecordsInfo findSendRecordsById(Long sendId);

    @Update("<script>" +
            "UPDATE `send_records_info` SET `send_id` = #{search.sendId}" +
            "<if test='search.reSend != null and search.reSend != \"\"'>,`re_send` = #{search.reSend}</if>" +
            "<if test='search.reSendId != null and search.reSendId != \"\"'>,`re_send_id` = #{search.reSendId} </if>" +
            "WHERE `send_id` = #{search.sendId}" +
            "</script>")
    Long updateSendRecords(@Param("search") SendRecordsInfo sendRecordsInfo);


}
